package com.caishi.lkx.user.model;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zzw.common.entity.impl.BaseIntEntity;
import com.zzw.common.service.model.NameModel;
import com.caishi.lkx.user.dto.GroupPermission;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;


/**
 * @Param: 机构管理修改为平台管理
 * @return
 * @Author: lkx
 * @Date: 2022/1/6 14:05
 * @Description:
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "sys_group", autoResultMap = true)
public class GroupModel extends BaseIntEntity<GroupModel> implements NameModel<GroupModel, Long> {

    @Schema(title = "平台名称")
    private String name;

    @Schema(title = "备注")
    private String remark;

    @Schema(title = "别名")
    private String alias;

    @Schema(title = "0:前台不展示别名 1:前台展示别名")
    private Boolean showAlias;

    @Schema(title = "有效期")
    private LocalDate expireTime;

    @Schema(title = "管理员id")
    private String managerId;

    @Schema(title = "水印图片")
    private String watermark;

    ///微信支付相关参数
    private String alipayAppId;
    private String alipayAppPrivateKey;
    private String alipayPublicKey;
    private String alipayAppPublicKey;
    private String alipayNotifyUrl;
    private String alipayReturnUrl;

    ///微信支付相关参数
    private String weixinpayAppId;
    private String weixinpayAppSecret;
    private String weixinpayMchId;
    private String weixinpaySignKey;
    private String weixinpayNotifyUrl;
    private String weixinpayCertPath;
    private String gzhAppId;
    private String gzhAppSecret;

    ///短信发送相关参数
    private String smsSignName;
    private String smsAccessKeyId;
    private String smsAccessKeySecret;
    private String smsEndpoint;

    ///银联支付相关地址
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private Map<String, Object> unionPayConfig;


    @TableField(typeHandler = FastjsonTypeHandler.class)
    private Map<String, Object> config;

    /**
     * 机构权限数据
     */
    @TableField(typeHandler = FastjsonTypeHandler.class, value = "permission")
    @JsonIgnore
    private List<?> permissionMap;

    @TableField(exist = false)
    private ChannelBusinessModel channelBusiness;

    public List<GroupPermission> getPermissionMap() {
        if (permissionMap == null) return null;
        return permissionMap.stream().map(v -> {
            if (v instanceof GroupPermission vg) return vg;
            if (v instanceof JSONObject vj) return vj.toJavaObject(GroupPermission.class);
            return new GroupPermission();
        }).toList();
    }

}
